<template>
  <Timer />
  <Diversion />
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import dayjs from "dayjs";
import useCommonStore from "@/store/common";
import useFlowStore from "@/store/flow.js";
import useAvtivityStore from "@/store/activity";
import useSeatStore from "@/store/seat";
import useHourStore from "@/store/hour";
import useAMANStore from "@/store/aman.js";
import useRunwayStore from "@/store/runway.js";
// import loadRecentFlowNote from "@/views/Journal/FlowNotes/loadRecentFlowNote.js";

import Timer from "@/views/Journal/Timer/main.vue";
import Diversion from "@/views/Journal/DiversionShare/Toolbox.vue";

const commonStore = useCommonStore();
const seatStore = useSeatStore();
const activityStore = useAvtivityStore();
const flowStore = useFlowStore();
const hourStore = useHourStore();
const AMANStore = useAMANStore();
const runwayStore = useRunwayStore();

// const isSeatAvailable = computed(() => seatStore.isSeatAvailable);
const isSeatAvailable = computed(() => commonStore.isReal);

const initialize = async () => {
  // client size
  commonStore.register();

  // waiting for getting seat ip
  // await seatStore.getCurrentSeat();

  // prepare for loading fake flow data
  if (!isSeatAvailable.value) {
    flowStore.customDate = "2024-08-03";
    flowStore.fakeDate = true;
  }

  // initial loading
  seatStore.requestData();
  activityStore.requestData();
  flowStore.loadSource();
  hourStore.load();
  AMANStore.loadParams();
  runwayStore.getRunwayStatus();
  // loadRecentFlowNote();

  // stop while not available
  if (!isSeatAvailable.value) return false;

  // Seat Status Auto Refresh
  setInterval(() => seatStore.requestData(), 10 * 1000);
  // Activity Status Auto Refresh
  setInterval(() => activityStore.requestData(), 20 * 1000);
  // Flow Status Auto Refresh
  setInterval(
    () => (flowStore.autoRefresh ? flowStore.loadSource() : null),
    20 * 1000
  );
  // load hours
  setInterval(() => hourStore.load(), 30 * 1000);
  // load AMAN params
  setInterval(() => AMANStore.loadParams(), 30 * 1000);
  //load Recent FlowNote
  // setInterval(() => loadRecentFlowNote(), 30 * 1000);
  // runwayStore
  setInterval(() => runwayStore.getRunwayStatus(), 20 * 1000);

  return true;
};

onMounted(() => initialize());
</script>
